<?php
/**
 * Description: 检测用户是否被合并
 * User: devforma
 * Date: 16/1/18
 * Time: 17:12
 */

class IsUserMerged implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        if (empty($queryArgs['user_id'])) {
            return genOutput(1000);
        }

        $merged = $this->checkUserMerged($queryArgs['user_id']);

        return genOutput(1, array('is_merged' => $merged));
    }

    /**
     * 校验用户是否被合并了
     *
     * @param $userId
     * @return bool
     */
    private function checkUserMerged($userId) {
        global $uddb;

        $cacheKey = 'allMergedUser';
        $mergedUsers = cacheGet($cacheKey);

        if (!$mergedUsers) {
            $allMergedUser = $uddb->getCol('SELECT `ID` FROM `user_auth_info` WHERE `merged_to`!=0');
            cacheAdd($cacheKey, $allMergedUser, 30*24*60*60); //30天的缓存
        }

        if (is_array($mergedUsers) && in_array($userId, $mergedUsers, true)) {
            return true;
        }

        return false;
    }
}